//
//  ZFK_scrollPicView.m
//  Drops
//
//  Created by SHG on 16/5/20.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "ZFK_scrollPicView.h"
#import "ZFK_scrollPic_CollectionViewCell.h"
#import "UIKit+AFNetworking.h"
//#import "ZFK_header.h"
#import "UIImageView+WebCache.h"
#import "ZFK_slideDetailViewController.h"
#import "DetailGalleryViewController.h"
#import "ZFK_webViewController.h"

@implementation ZFK_scrollPicView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
}
    return self;
}

- (void)createView {
    
//    self.owner = self;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(sWIDTH, sHEIGHT / 2 - sHEIGHT / 7);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, sHEIGHT / 2 - sHEIGHT / 7) collectionViewLayout:layout];
    self.mainCollectionView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.mainCollectionView];
    
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    
    [self.mainCollectionView registerClass:[ZFK_scrollPic_CollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ZFK_scrollPic_CollectionViewCell class])];
    
    self.mainCollectionView.pagingEnabled = YES;
    self.mainCollectionView.showsHorizontalScrollIndicator = NO;
    
    [self addSubview:self.mainCollectionView];
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 30, self.frame.size.width, 30)];
    self.page.hidesForSinglePage = YES;
    [self addSubview:self.page];
    
    self.page.backgroundColor = [UIColor clearColor];
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.page.pageIndicatorTintColor = [UIColor lightGrayColor];
    
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

    self.page.numberOfPages = self.scrollPicArray.count - 2;
    
    self.page.currentPage = self.mainCollectionView.contentOffset.x / sWIDTH - 1;
    
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(pageChange) userInfo:nil repeats:YES];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.scrollPicArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZFK_scrollPic_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZFK_scrollPic_CollectionViewCell class]) forIndexPath:indexPath];
    self.header = [[ZFK_header alloc] init];
    [self.header setValuesForKeysWithDictionary:self.scrollPicArray[indexPath.row]];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.header.activityImage]];
    cell.titleLabel.text = self.header.title;
    self.page.numberOfPages = self.scrollPicArray.count - 2;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.mainCollectionView.contentOffset.x == 0) {
        self.mainCollectionView.contentOffset = CGPointMake((self.scrollPicArray.count - 2) * sWIDTH, 0);
    } else if (self.mainCollectionView.contentOffset.x == (self.scrollPicArray.count - 1) * sWIDTH) {
        self.mainCollectionView.contentOffset = CGPointMake(sWIDTH, 0);
    }
    self.page.currentPage = self.mainCollectionView.contentOffset.x / sWIDTH - 1;
}

- (void)pageAction:(UIPageControl *)page {
    self.mainCollectionView.contentOffset = CGPointMake(sWIDTH * (page.currentPage + 1), 0);
    
    [self.mainCollectionView setContentOffset:CGPointMake((sWIDTH * (page.currentPage + 1)), 0) animated:YES];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"轮播图点击:%ld", indexPath.row);
    NSLog(@"ID:%@", self.header.activityId);

    [self createData];
    self.owner = [[UIViewController alloc] init];
    self.owner = [self viewController];
    [self createFlower];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        ZFK_slideDetailViewController *slideVC = [[ZFK_slideDetailViewController alloc] init];
        slideVC.activityId = self.header.activityId;
        slideVC.imgUrl = self.imgUrl;
        slideVC.text = self.text;
        slideVC.titled = self.titled;
        slideVC.userName = self.userName;
        slideVC.avatar = self.avatar;
//        self.owner = [[UIViewController alloc] init];
//        self.owner = [self viewController];
        
        ZFK_webViewController *webVC = [[ZFK_webViewController alloc] init];
        webVC.url = self.url;
        
//        NSLog(@"title:%@",slideVC.titled);
//        NSLog(@"=======%@", self.imgUrl);
        NSLog(@"-----%@", self.url);
        
        if ([self.url isEqualToString:@""]) {
            [self.owner.navigationController pushViewController:slideVC animated:YES];
        } else {
            [self.owner.navigationController pushViewController:webVC animated:YES];
        }
//        [self.owner.navigationController pushViewController:slideVC animated:YES];
        
        self.hud.hidden = YES;
        
    });
    
}

- (void)createData {
    NSDictionary *param = @{@"version":@"2.9", @"uid":@"25512642", @"token":@"e89ba406540046ccbfc816bb142cda852bc282b04bc844458cf9bf29899028b8", @"activityId":[NSString stringWithFormat:@"%@", self.header.activityId], @"sortby":@"time", @"sincePostId":@"0", @"lastLikeCount":@"", @"offset":@"0"};
    
    [[AFHTTPSessionManager manager] POST:@"http://www.fotoplace.cc/api2/home/activity_detail.php" parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.slideArr = [NSMutableArray array];
        
        NSDictionary *tempDic = responseObject[@"data"][@"activity"];
        self.imgUrl = [NSString stringWithFormat:@"%@", tempDic[@"imgUrl"]];
        self.titled = [NSString stringWithFormat:@"%@", tempDic[@"title"]];
        self.avatar = [NSString stringWithFormat:@"%@", tempDic[@"avatar"]];
        self.userName = [NSString stringWithFormat:@"%@", tempDic[@"userName"]];
        self.text = [NSString stringWithFormat:@"%@", tempDic[@"text"]];
        self.url = [NSString stringWithFormat:@"%@", tempDic[@"url"]];
        NSLog(@"urllll:%@", self.imgUrl);
        
        NSLog(@"dic--%@", [tempDic[@"personCount"] class]);
        [self.mainCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        
    }];
}

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (void)createFlower {
        self.hud = [[MBProgressHUD alloc] init];
        //    self.hud.labelText = @"你好";
        [self.owner.view addSubview:self.hud];
        [self.hud show:YES];
}

- (void)pageChange {
    [self.mainCollectionView setContentOffset:CGPointMake(self.mainCollectionView.contentOffset.x + sWIDTH, 0) animated:YES];
    if (self.mainCollectionView.contentOffset.x == (self.scrollPicArray.count - 1) * sWIDTH) {
        self.mainCollectionView.contentOffset = CGPointMake(sWIDTH, 0);
    }
    self.page.currentPage = self.mainCollectionView.contentOffset.x / sWIDTH - 1;
}

- (void)setScrollPicArray:(NSMutableArray *)scrollPicArray{
    _scrollPicArray = scrollPicArray;
    self.mainCollectionView.contentOffset = CGPointMake(sWIDTH, 0);
    [self.mainCollectionView reloadData];
}

@end
